package de.bazinga.rotw.input;

import com.jme.input.Mouse;
import com.jme.input.MouseInput;
import com.jme.input.action.InputActionEvent;
import com.jme.input.action.KeyRotateLeftAction;
import com.jme.input.action.KeyRotateRightAction;
import com.jme.input.action.MouseInputAction;
import com.jme.math.Vector3f;
import com.jme.renderer.Camera;

public class LockedMouseLook extends MouseInputAction {

    //actions to handle looking up down left and right.
    private KeyLookUpAndDownAction lookUpAndDown;


    private KeyRotateLeftAction rotateLeft;

    private KeyRotateRightAction rotateRight;
    //the axis to lock.
    private Vector3f lockAxis;
    //the event to distribute to the looking actions.
    private InputActionEvent event;
    
    private boolean buttonPressRequired = false;
    
    private int mouseButtonForRequired = 0;

    /**
     * Constructor creates a new <code>MouseLook</code> object. It takes the
     * mouse, camera and speed of the looking.
     * 
     * @param mouse
     *            the mouse to calculate view changes.
     * @param camera
     *            the camera to move.
     * @param speed
     *            the speed at which to alter the camera.
     */
    public LockedMouseLook(Mouse mouse, Camera camera, float speed) {
        this.mouse = mouse;
        this.speed = speed;

        lookUpAndDown = new KeyLookUpAndDownAction(camera, speed);
        rotateLeft = new KeyRotateLeftAction(camera, speed);
        rotateRight = new KeyRotateRightAction(camera, speed);

        event = new InputActionEvent();
    }

    /**
     * 
     * <code>setLockAxis</code> sets the axis that should be locked down. This
     * prevents "rolling" about a particular axis. Typically, this is set to the
     * mouse's up vector. Note this is only a shallow copy.
     * 
     * @param lockAxis
     *            the axis that should be locked down to prevent rolling.
     */
    public void setLockAxis(Vector3f lockAxis) {
        this.lockAxis = lockAxis;
        rotateLeft.setLockAxis(lockAxis);
        rotateRight.setLockAxis(lockAxis);
    }

    /**
     * Returns the axis that is currently locked.
     * 
     * @return The currently locked axis
     * @see #setLockAxis(com.jme.math.Vector3f)
     */
    public Vector3f getLockAxis() {
        return lockAxis;
    }

    /**
     * 
     * <code>setSpeed</code> sets the speed of the mouse look.
     * 
     * @param speed
     *            the speed of the mouse look.
     */
    public void setSpeed(float speed) {
        super.setSpeed( speed );
        lookUpAndDown.setSpeed(speed);
        rotateRight.setSpeed(speed);
        rotateLeft.setSpeed(speed);
    }

    /**
     * <code>performAction</code> checks for any movement of the mouse, and
     * calls the appropriate method to alter the camera's orientation when
     * applicable.
     * 
     * @see com.jme.input.action.MouseInputAction#performAction(InputActionEvent)
     */
    public void performAction(InputActionEvent evt) {
        float time = 0.01f * speed;

        if(!buttonPressRequired || MouseInput.get().isButtonDown(mouseButtonForRequired)) {
            if (mouse.getLocalTranslation().x > 0) {
                event.setTime(time * mouse.getLocalTranslation().x);
                rotateRight.performAction(event);
            } else if (mouse.getLocalTranslation().x < 0) {
                event.setTime(time * mouse.getLocalTranslation().x * -1);
                rotateLeft.performAction(event);
            }
            event.setTime(time * mouse.getLocalTranslation().y);
            lookUpAndDown.performAction(event);
            
        }

    }

    /**
	 * @return the mouseButtonForRequired
	 */
	public int getMouseButtonForRequired() {
		return mouseButtonForRequired;
	}

	/**
	 * @param mouseButtonForRequired the mouseButtonForRequired to set
	 */
	public void setMouseButtonForRequired(int mouseButtonForRequired) {
		this.mouseButtonForRequired = mouseButtonForRequired;
	}

	public boolean isButtonPressRequired() {
        return buttonPressRequired;
    }

    public void setButtonPressRequired(boolean buttonPressRequired) {
        this.buttonPressRequired = buttonPressRequired;
    }
}